import re
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from chaojiying_Python.chaojiying import Chaojiying_Client


class Keyword:

    def __init__(self):
        self.driver:webdriver.Chrome=None
        self.relation_dict={}

    def openBrower(self,br=''):
        option = Options()
        option.add_experimental_option('excludeSwitches', ['enable-automation'])
        option.add_argument("--disable-blink-features=AutomationControlled")
        # 实现不关闭的重点
        option = webdriver.ChromeOptions()
        option.add_experimental_option("detach", True)
        # 关闭密码弹框：
        option.add_experimental_option("prefs", {
            "download.prompt_for_download": False,
            "download.directory_upgrade": True,
            "safebrowsing.enabled": True,
            "credentials_enable_service": False,
            "profile.password_manager_enabled": False
        })
        option.add_experimental_option("excludeSwitches", ['enable-automation'])
        if br=='gc'or br=='':
            self.driver=webdriver.Chrome(options=option)
        if br=='ff':
            self.driver=webdriver.Firefox(options=option)
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    #访问网站
    def visiturl(self,url=''):
        self.driver.get(url)

    # 找到元素
    def findelement(self,locator=''):
        ele = self.driver.find_element(By.XPATH, locator)
        return ele

    #查找元素
    def __find_element(self,locator=''):
        ele=self.driver.find_element(By.XPATH,locator)
        return ele

    #输入操作
    def input(self,locator='',value=''):
        ele=self.__find_element(locator)
        ele.clear()
        ele.send_keys(value)

    # js输入操作
    def js_input(self, locator='', value=''):
        ele = self.__find_element(locator)
        self.driver.execute_script("arguments[0].value=%s"%value,ele)


    #点击操作
    def click(self,locator=''):
        ele=self.__find_element(locator)
        ele.click()

    #js点击操作
    def js_click(self,locator=''):
        ele=self.__find_element(locator)
        self.driver.execute_script("arguments[0].click()",ele)

    #等待操作
    def sleep(self,sleeptime=''):
        num=float(sleeptime)
        time.sleep(num)

    #切换Iframe
    def swich_iframe(self,locator=''):
        ele=self.__find_element(locator)
        self.driver.switch_to.frame(ele)

    #返回iframe
    def swich_defauliframe(self):
        self.driver.switch_to.default_content()


    #封装select
    def select(self,locator='',value=''):
        ele=self.__find_element(locator)
        select=Select(ele)
        select.select_by_visible_text(value)

    #封装悬停操作
    def movetoelement(self,locator=''):
        ele=self.__find_element(locator)
        action=ActionChains(self.driver)
        action.move_to_element(ele)
        action.perform()

    #滚动到底部
    def scroll_top(self):
        js="var q=document.documentElement.scrollTop=10000"
        self.driver.execute_script(js)

    # 滚动到底部
    def scroll_below(self):
        js = "var q=document.documentElement.scrollTop=0"
        self.driver.execute_script(js)

    #滚动一段距离
    def scroll_distince(self,distince=''):
        num=float(distince)
        js="window.scrollTo(0,%d)"%num
        self.driver.execute_script(js)

    #正则表达式获取值
    def getvalueByregular(self,text='',regular=''):
        result=re.findall(regular,text)
        return result[0]

    #获取文本值
    def gettext(self,locator='',key=''):
        text=self.__find_element(locator).text
        return text

    #根据title切换窗口
    def swich_window(self,title=''):
        handles=self.driver.window_handles
        for handle in handles:
            self.driver.switch_to.window(handle)
            if self.driver.title==title:
                break

    #返回初始窗口
    def swich_defaulwindow(self):
        self.driver.switch_to.default_content()


    #根据元素截图
    def screenshotByelement(self,locator='',filename=''):
        ele=self.__find_element(locator)
        ele.screenshot(filename)

    # 全屏截图
    def screenshotAll(self,filename=''):
        self.driver.save_screenshot(filename)

    # 获取图像文字
    def verify(self,locator='',filename='',code=1902):
        self.screenshotByelement(locator,filename)
        chaojiying = Chaojiying_Client('Ww1094622592', '5732486angel', '934965')  # 用户中心>>软件ID 生成一个替换 96001
        im = open(filename, 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
        verify=chaojiying.PostPic(im, code).get('pic_str')
        print(chaojiying.PostPic(im, code).get('pic_str'))
        self.sava_param('verify',verify)
        print(self.relation_dict)
        return  verify



    #12306拖拉滑块操作
    def draw_and_drop(self,small_locator='',large_locator=''):
        small_ele = self.findelement(small_locator)
        small_width = small_ele.size.get('width')
        large_ele = self.findelement(large_locator)
        large_width = large_ele.size.get('width')
        offset_value = large_width - small_width
        action=ActionChains(self.driver)
        action.drag_and_drop_by_offset(small_width,offset_value,0)
        action.perform()

    #关闭浏览器
    def close(self):
        self.driver.close()

    #退出浏览器
    def quit(self):
        self.driver.quit()


    #存入字典
    def sava_param(self,key='',value=''):
        self.relation_dict[key]=value
























